package com.lagou.web;

import com.lagou.domain.Student;
import com.lagou.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer number = Integer.parseInt(req.getParameter("number"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(birthday);
            Student student = new Student(number, name, sex, date);

            if (StudentService.addStudent(student)) {
                resp.getWriter().write("添加学生信息成功");
            } else {
                resp.getWriter().write("添加学生信息失败");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            resp.getWriter().write("日期格式错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
